//
//  SearchVC.m
//  NeteaseNewsLY
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SearchVC.h"
#import "SearchSelectVC.h"

@interface SearchVC ()<UISearchResultsUpdating, UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate, UISearchControllerDelegate>
// 搜索控制视图
@property (nonatomic, retain)UISearchController *search;
// 高亮用字符串 保存搜索信息
@property (nonatomic, copy)NSString *strHight;
// 接收数据数组
@property (nonatomic, retain)NSMutableArray *dataArr;
// 显示搜索数据内容
@property (nonatomic, retain)UITableView *tableView;
@end

@implementation SearchVC
- (void)dealloc
{
    [_search release];
    [_strHight release];
    [_dataArr release];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 返回
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(fanhui)];
    
    //初始化搜索视图
    self.search = [[UISearchController alloc]initWithSearchResultsController:nil];
    //签订协议
    _search.searchResultsUpdater = self;
    //屏幕变灰 yes时tabView 不能下拉
    _search.dimsBackgroundDuringPresentation = NO;
    //设置点击搜索框时 隐藏导航栏
    _search.hidesNavigationBarDuringPresentation = NO;
    //占位符 默认search
    _search.searchBar.placeholder = @"搜索感兴趣的内容";
    
    //搜索栏的风格
    _search.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    
    //把搜索视图加到导航栏上
    self.navigationItem.titleView = _search.searchBar;
    [_search release];
    
    
    //创建tableView
    [self creatTabeleView];

}

// 创建tableView
- (void)creatTabeleView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, WIDTH * 55, 0);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    // 将搜索内容转化为字符串
    // 等同于 获取搜索的信息 显示的文本
    self.strHight = [self.search.searchBar text];
    if ([_strHight containsString:@" "]&& _dataArr.count == 0) {
        
        // 判断如果有空格不执行
        // 没有执行以下程序
        //不写这个判断 打空格就会崩溃
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"没有您要搜索的内容" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }else{
        //中文转码
        NSString *urlStr = [_strHight stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:_strHight]];
        
        //变化的参数进行拼接
        NSString *str = [@"http://c.3g.163.com/search/comp/MA%3D%3D/20/5paw6Ze7.html?deviceId=MzU5Nzg2MDU3MjcwMDAy&version=bmV3c2NsaWVudC41LjQuNy5hbmRyb2lk&channel=VDEzNDg2NDc5MDkxMDc%3D" stringByReplacingOccurrencesOfString:@"5paw6Ze7.html" withString:[NSString stringWithFormat:@"%@.html", urlStr]];
        [AFHTTPHandler GETWithURL:str completion:^(id result) {
            NSArray *arr = result[@"doc"][@"result"];
            self.dataArr = [SearchModel modelHandleWithArray:arr];
            [self.tableView reloadData];
            
        }];
    }
}
// 赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    SearchModel *m = _dataArr[indexPath.row];
    // 高亮设置
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:m.title];
    //取范围
    NSRange range = [m.title rangeOfString:_strHight];
    [attStr setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName: [UIFont systemFontOfSize:20]} range:range];
    cell.textLabel.attributedText = attStr;
    
    return cell;


}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"搜索成功~~~");
    SearchSelectVC *sousuo = [[SearchSelectVC alloc] init];
    SearchModel *m = _dataArr[indexPath.row];
    sousuo.urlStr = m.docid;
    [self.navigationController pushViewController:sousuo animated:YES];
    [sousuo release];
  
    
}
- (void)fanhui
{
   [self dismissViewControllerAnimated:YES completion:^{
       
   }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
